package com.example.music.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.music.entity.ListSong;
import com.example.music.service.ListSongService;
import com.example.music.utils.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("listsong")
public class ListSongController {
    @Autowired
    ListSongService listSongService;

    @PostMapping("add")
    public AjaxJson addListSong(@RequestParam Integer songId,Integer songListId){
        ListSong listSong = new ListSong();
        listSong.setSongId(songId);
        listSong.setSongListId(songListId);
        Integer add = listSongService.add(listSong);
        return AjaxJson.getSuccess(add.toString());
    }

    @PostMapping("test")
    public AjaxJson test(@RequestParam Integer songId,Integer songListId){
        return AjaxJson.getSuccess();
    }

    @PostMapping("ListSongById")
    public AjaxJson ListSongById(@RequestParam Integer songListId){
        List<ListSong> listSongs = listSongService.ListSongById(songListId);
        return AjaxJson.getSuccessData(listSongs);
    }

    @PostMapping("update")
    public AjaxJson updateListSong(@RequestBody ListSong listSong){
        UpdateWrapper<ListSong> listSongUpdateWrapper = new UpdateWrapper<>();
        listSongUpdateWrapper.eq("id",listSong.getId());
        int update = listSongService.update(listSong, listSongUpdateWrapper);
        return AjaxJson.getSuccess(String.valueOf(update));
    }

    @PostMapping("delete")
    public AjaxJson deleteListSong(@RequestParam String songId,String songListId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("song_id",songId);
        map.put("song_list_id",songListId);
        listSongService.deleteByParams(map);
        return AjaxJson.getSuccess();
    }
}
